package com.golxen.framework.common.server.game.dao;


import com.alibaba.fastjson.JSONObject;
import com.golxen.framework.db.dao.AbstractDaoImpl;
import com.golxen.framework.common.server.game.dao.entity.CrossServerInfo;
import com.golxen.framework.common.server.game.dao.entity.DbInfo;
import com.golxen.framework.common.server.game.dao.entity.ProcessInfo;
import com.golxen.framework.common.server.game.dao.entity.ServerCrossRankServer;
import com.golxen.framework.db.DBManager;
import com.mysql.cj.util.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class CrossServerInfoDaoImpl extends AbstractDaoImpl {
    public CrossServerInfo getCrossServerInfo(int channelId, int serverId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_cross_server_list where channelId = ? and crossServerId = ?";
        try {
            getDb().setConnName(DBManager.serverDbName);
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, channelId);
            call.setInt(2, serverId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parseCrossServerInfo(serverId, rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public List<CrossServerInfo> getAllChannelCrossServerInfo(int channelId) {
        List<CrossServerInfo> crossServerInfoList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_cross_server_list where channelId = ? ";
        try {
            getDb().setConnName(DBManager.serverDbName);
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, channelId);
            rs = call.executeQuery();
            while (rs.next()) {
                int serverId = rs.getInt("crossServerId");
                CrossServerInfo crossServerInfo = parseCrossServerInfo(serverId, rs);
                crossServerInfoList.add(crossServerInfo);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return crossServerInfoList;
    }

    private CrossServerInfo parseCrossServerInfo(int serverId, ResultSet rs) throws SQLException {
        CrossServerInfo crossServerInfo = new CrossServerInfo();
        crossServerInfo.setCrossServerId(serverId);
        DbInfo dbInfo = parseDbInfo(rs.getString("crossDatabase"));
        if (dbInfo != null) {
            crossServerInfo.getDbInfos().add(dbInfo);
        }
        DbInfo configbase = parseDbInfo(rs.getString("crossConfigbase"));
        if (configbase != null) {
            crossServerInfo.getDbInfos().add(configbase);
        }

        crossServerInfo.setCrossServerType(rs.getInt("crossServerType"));

        ProcessInfo crossProcessInfo = ProcessInfo.parsingProcessInfo(rs.getString("crossServerInfo"), serverId);
        crossServerInfo.getProcessInfoList().add(crossProcessInfo);
        int rankStatus = rs.getInt("isRankServer");
        crossServerInfo.setRankServer(rankStatus != 0);
        return crossServerInfo;
    }

    public List<Long> getServerIdByCrossRankServerId(long crossRankServerId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select serverId from t_s_server_cross_rank_server where crossRankServerId  = ?";
        List<Long> data = new ArrayList<Long>();
        try {
            getDb().setConnName(DBManager.serverDbName);
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, crossRankServerId);
            rs = call.executeQuery();
            while (rs.next()) {
                data.add(rs.getLong("serverId"));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return data;
    }

    public ServerCrossRankServer getCrossRankServerId(long serverId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_server_cross_rank_server where serverId  = ?";
        try {
            getDb().setConnName(DBManager.serverDbName);
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, serverId);
            rs = call.executeQuery();
            while (rs.next()) {
                ServerCrossRankServer serverCrossRankServer = new ServerCrossRankServer();
                serverCrossRankServer.setServerId(rs.getInt("serverId"));
                serverCrossRankServer.setCrossRankServerId(rs.getInt("crossRankServerId"));
                serverCrossRankServer.setCrossCenterServerId(rs.getInt("crossCenterServerId"));
                serverCrossRankServer.setCrossBanquetServerId(rs.getInt("crossBanquetServerId"));

                return serverCrossRankServer;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public Set<Integer> getCross2SpecialServerIdList() {
        ResultSet rs = null;
        CallableStatement call = null;
        // 为0表示走随机，不指派
        String sql = "select DISTINCT(crossBanquetServerId) as serverId from t_s_server_cross_rank_server where crossBanquetServerId != 0 ";

        Set<Integer> serverIds = new HashSet<Integer>();
        try {

            getDb().setConnName(DBManager.serverDbName);
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                serverIds.add(rs.getInt("serverId"));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return serverIds;
    }

    private DbInfo parseDbInfo(String str) {
        if (StringUtils.isNullOrEmpty(str)) {
            return null;
        }

        JSONObject jObject = null;
        try {
            jObject = JSONObject.parseObject(str);
            DbInfo info = new DbInfo();
            info.setDbName(jObject.getString("dbname"));
            info.setUserName(jObject.getString("dbuser"));
            info.setIp(jObject.getString("ip"));
            info.setPassword(jObject.getString("dbpassword"));
            info.setPort(jObject.getIntValue("port"));

            return info;
        } catch (Exception e) {
            return null;
        }
    }


}
